<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>物料类型编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
<script type="text/javascript">
var ItemStateList=ItemStateData.Rows;
var basic_Url="${ctx}/basic/series";
var materGrid;
var process;
$(function(){
$("#inputForm").validate({			   
	onsubmit: true,
	 /*执行ajaxsubmit  */ 
    submitHandler: function(editform) {
            
        //alert(jsonInfo); 
        try{			    		
            url =  basic_Url+"/save.do";
            var api = frameElement.api;
        	var W = api.opener;
            getReturnMessage(url,"inputForm",W.searchAP);	
        }catch(e){alert(e);}
    }
 });
 //原材料列表
materGrid=$("#table1").ligerGrid({
    columns: [
				{ display: '原材料ID', name: 'zd003MaterialsId', hide:true },
				{ display: '原材料类型ID', name: 'zd004MaterTypeId', hide:true },
                { display: '<font color="gray">原材料编码', name: 'zd001Code', width: 120,
              	  render: function (item){
            		  if(item.basicMaterialsDO==null){
            			  return item.zd001Code;
            		  }else{
            			  return item.basicMaterialsDO.zd001Code;
            		  }
            	  }},
                  { display: '<font color="gray">原材料名称</font>', name: 'zd002Name', width: 120,
                	  render: function (item){
                		  if(item.basicMaterialsDO==null){
                			  return item.zd002Name;
                		  }else{
                			  return item.basicMaterialsDO.zd002Name;
                		  }
                	  }},
                  { display: '<font color="gray">型号</font>', name: 'zd004Model', width: 120,
                    	  render: function (item){
                    		  if(item.basicMaterialsDO==null){
                    			  return item.zd004Model;
                    		  }else{
                    			  return item.basicMaterialsDO.zd004Model;
                    		  }
                 }},
                { display: '<font color="gray">尺寸</font>', name: 'zd005Size', width: 120,
                    	  render: function (item){
                    		  if(item.basicMaterialsDO==null){
                    			  return item.zd005Size;
                    		  }else{
                    			  return item.basicMaterialsDO.zd026Size;
                    		  }
                 }},
                { display: '<font color="gray">单位</font>', name: 'zd007Unit', width: 80,
                        	  render: function (item){
                        		  if(item.basicMaterialsDO==null){
                        			  return item.zd007Unit;
                        		  }else{
                        			  return item.basicMaterialsDO.basicStapleUnitDO.zd002Name;
                        		  }
                }},
               { display: '工序名称', name: 'zd002ProcessName', width: 100,render: function (item){
         		  if(item.basicProcessDO==null){
        			  return item.zd002ProcessName;
        		  }else{
        			  return item.basicProcessDO.zd002Name;
        		  }
        	  	}},
                { display: '工序ID', name: 'zd002ProcessId',hide:true},
                { display: '材料状态<font color="red">*</font>', name: 'zd011UseMaterialsType', width: 100,editor: { type: 'select', data: ItemStateList, valueColumnName: 'ItemStateID', displayColumnName: 'ItemStateName'}, render: function (item)
        	        {
        	        for (var i = 0; i < ItemStateList.length; i++)
        	        {
        	            if (ItemStateList[i]['ItemStateID'] == item.zd011UseMaterialsType){
        	          	  return ItemStateList[i]['ItemStateName'];
        	            }
        	        }
        	        return item.zd011UseMaterialsType;
				}},
                { display: '用量<font color="red">*</font>', name: 'zd005Dosage', width: 100,editor : {type :'float'}},
                 { display: '备注', name: 'zd016Remark', width: 180,editor : {type :'text'}}
                  ],  usePager: true,pageSize:1000,pageSizeOptions :[1000],enabledEdit : true,
                  url : "${ctx}/basic/materbom/materList.do?ID=XL${series.id}",enabledSort:false,
                  width: "100%", height:200, checkbox: true, 
                  toolbar: { items: [
									  { text: '选择原材料', click: selectYCLItem, icon: 'add' },
                                      { line: true },
                                      { text: '工序', click: selectMTGXItem, icon: 'add' },
                                      { line: true },
                                      { text: '删除', click: null, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                      ]
                                      }
              });
 //工序列表
process=$("#table2").ligerGrid({
    columns: [
              		{ display: '工序ID', name: 'zd002ProcessId',hide:true},
                  { display: '工序名称', name: 'zd002Name', width: 120,
                  	  render: function (item){
                		  if(item.basicProcessDO==null){
                			  return item.zd002Name;
                		  }else{
                			  return item.basicProcessDO.zd002Name;
                		  }
                	  }},
                  { display: '工序编码', name: 'zd001Code', width: 120,
                    	  render: function (item){
                    		  if(item.basicProcessDO==null){
                    			  return item.zd001Code;
                    		  }else{
                    			  return item.basicProcessDO.zd001Code;
                    		  }
                    	  }},
                  { display: '工序车间', name: 'zd001Name', width: 120,
                        	  render: function (item){
                        		  if(item.basicProcessDO==null){
                        			  return item.zd001Name;
                        		  }else{
                        			  return item.basicProcessDO.basicWorkshopDO.zd002Name;
                        		  }
                        	  }},
                  { display: '单件工时', name: 'zd006TaktTime', width: 120,editor : {type :'float'}},
                  { display: '委外面积', name: 'zd007OutsourcingArea', width: 120,editor : {type :'float'}},
                  { display: '尾料重量', name: 'zd008TailWeight', width: 120,editor : {type :'float'}},
                  { display: '委外单价', name: 'zd009OutsourcingPrice', width: 120,editor : {type :'float'}},
                  { display: '加工单价', name: 'zd010ProcessingPrice', width: 120,editor : {type :'float'}}	  
                  ],  usePager: true,pageSize:1000,pageSizeOptions :[1000],enabledEdit : true,
                  url : "${ctx}/basic/materbom/processList.do?ID=XL${series.id}",enabledSort:false,
                  width: "100%", height:200, checkbox: true, 
                  toolbar: { items: [
									  { text: '增加工序', click: selectGXItem, icon: 'add' },
                                      { line: true },
                                      { text: '删除', click: null, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                      ]
                                      }
              });  
});

function submitForm(param){
	var data = $("#table1").ligerGetGridManager().getData();//物料信息
	if(data.length==0){
		$.dialog.alert("请增加物料信息");
   	 	return false;
	}
	var ba=true;
	if(data !=null){
		for (var i = 0;i < data.length;i++){
			var row = data[i];
			
			if (undefined == row.zd005Dosage|| null == row.zd005Dosage||row.zd005Dosage=="") {
				$.dialog.alert("第" + (i+1) + "行的用量没有选择");
				ba = false;
				return false;
			}
		}
	}
		if(ba){
			$("#materItem").val(JSON.stringify(data));
			$("#processItem").val(JSON.stringify($("#table2").ligerGetGridManager().getData()));
			jQuery.ajaxSettings.async = false;
		  	$("#submitFlag").val(param);
		    $("#inputForm").submit();
		}
	
 }

//选择原材料
function selectYCLItem(){
	getDG().dialog({
		title:'选择原材料',
		width : 800,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/itemmanager.do',//
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var rows = this.content.getSelected();
			if (rows != null) {
				for (var i = 0; i < rows.length;i++){
					var row = rows[i];
					materGrid.addRow2({
						zd003MaterialsId : row.id,
						zd001Code : row.zd001Code,
						zd002Name : row.zd002Name,
						zd005Size : row.zd026Size,
						zd004Model : row.zd004Model,
						zd007Unit : row.basicStapleUnitDO.zd002Name,
						zd004MaterTypeId : row.basicMaterialsTypeDO.id
					});	
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}
//选择工序
function selectGXItem(){
	getDG().dialog({
		title:'选择工序',
		width : 800,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/process.do',//
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var rows = this.content.getSelected();
			if (rows != null) {
				for (var i = 0; i < rows.length;i++){
					var row = rows[i];
					process.addRow2({
						zd002ProcessId : row.id,
						zd001Code : row.zd001Code,
						zd002Name : row.zd002Name,
						zd001Name : row.basicWorkshopDO.zd002Name
					});	
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}

//关联物料选择工序
function selectMTGXItem(){
	var mrows = materGrid.getCheckedRows();
	if(mrows.length==0){
		$.dialog.alert("请选择需要关联工序的物料！");
   	 	return false;
	}
	getDG().dialog({
		title:'选择工序',
		width : 600,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/process.do',//
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var rows = this.content.getSelected();
			if (rows != null) {
				for (var i = 0; i < rows.length;i++){
					var row = rows[0];
					materGrid.updateCell('zd002ProcessId', row.id, mrows[0]);
					materGrid.updateCell('zd002ProcessName', row.zd002Name, mrows[0]);
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${series.id}"/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${series.bizVersion}"/>
<input type="hidden" id="materItem" name="materItem" value=""/>
<input type="hidden" id="processItem" name="processItem" value=""/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
 <div class="pageContent" >
    <div class="panel">
         </div>
         <div title="物料列表" id="table1"></div>  
         <div title="工序列表" id="table2"></div>  
         <div class="formBar">
	        <ul>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm(1)">保存</button></div></div></li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
